import Store from 'electron-store'

const schema = {
    settings: {
        type: 'object',
        properties: {
            theme: {
                type: 'string',
                enum: ['system', 'light', 'dark']
            },
            saveLoginInfo: {
                type: 'boolean'
            }
        }
    },
    chatlist: {
        type: 'array',
        items: {
            type: 'object',
            properties: {
                type: {
                    type: 'string'
                },
                id: {
                    type: 'string'
                },
                username: {
                    type: 'string'
                },
                nickname: {
                    type: 'string'
                },
                avartar: {
                    type: 'string',
                    description: '头像链接'
                },
                message: {
                    type: 'string'
                },
                time: {
                    type: 'string'
                },
                unreadNum: {
                    type: 'number',
                    description: '未读消息数'
                }
            }
        }
    }
}

const defaults = {
    settings: {
        theme: 'system',
        saveLoginInfo: true
    },
    chatlist: []
}

const store = new Store({
    schema,
    defaults
    // encryptionKey: import.meta.env.MAIN_VITE_ENCRYPTIONKEY
})

export default {
    getAllSettings: () => store.get('settings'),
    getSetting: (key) => store.get('settings.' + key),
    setSetting(key, value) {
        store.set('settings.' + key, value)
    },
    getChatlist: () => store.get('chatlist'),
    setChatlist: (value) => {
        store.set('chatlist', value)
    }
}
